package aj.service.impl;

import aj.bo.AjActivationCode;
import aj.bo.AjJs;
import aj.dao.IAjJsDao;
import aj.service.AjActivationCodeService;
import aj.service.AjJsService;
import framework.common.Common;
import framework.util.layui._LayuiUtil;
import framework.util.queryFilter.service.impl.IBaseServiceImpl;
import javax.annotation.Resource;

import framework.util.queryFilter.service.impl.QueryFilter;
import framework.util.queryFilter.vo.Where;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("AjJsService")
public class AjJsServiceImpl extends IBaseServiceImpl<AjJs, Integer> implements AjJsService {

    @Resource
    private AjActivationCodeService ajActivationCodeService;
    @Resource
    private IAjJsDao dao;

    @Override
    public void add(AjJs js, List<AjActivationCode> oldCodeList) {
        //保存新版本
        this.add(js);

        //为旧用户生成新版本的激活码
        for (AjActivationCode oldCode : oldCodeList) {
            AjActivationCode newCode = new AjActivationCode();
            newCode.setJsLid(js.getLid());
            newCode.setPhoneImei(oldCode.getPhoneImei());
            newCode.setPhoneBrand(oldCode.getPhoneBrand());
            newCode.setIsUse(Common.IS_YEW);
            newCode.setIsUpgrade(Common.IS_YEW);
            ajActivationCodeService.add(newCode);
        }
    }

    @Override
    public void deleteByJmLid(Integer jmLid) {
        QueryFilter filter = new QueryFilter(_LayuiUtil.FIRST_PAGE, Integer.MAX_VALUE);
        filter.where(AjJs.JM_LID, Where.eq, jmLid);
        List<AjJs> list = this.getAll(filter);

        list.forEach(item -> item.setIsDelete(Common.IS_YEW));
        this.updateBatch(list);
    }
}

